home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q1082.dms / q1082.adf / src.lzh / Fig / curve.c < prev    next >
C/C++ Source or Header  |  1991-07-18  |  2KB  |  84 lines

  1. /* 
  2.  *    FIG : Facility for Interactive Generation of figures
  3.  *
  4.  *    Copyright (c) 1985 by Supoj Sutanthavibul (supoj@sally.UTEXAS.EDU)
  5.  *    January 1985.
  6.  *    1st revision : Aug 1985.
  7.  *
  8.  *    %W%    %G%
  9. */
  10. #include "fig.h"
  11. #include "resources.h"
  12. #include "paintop.h"
  13.  
  14. #define        round(a)    ((int)((a) + .5))
  15.  
  16. /*    This routine plot two dimensional curve defined by a second degree
  17.     polynomial of the form :
  18.                 2    2
  19.         f(x, y) = ax + by + g = 0
  20.  
  21.     (x0,y0) is the starting point as well as ending point of the curve.
  22.     The curve will translate with the offset xoff and yoff.
  23.  
  24.     This algorithm is derived from the eight point algorithm in :
  25.     "An Improved Algorithm for the generation of Nonparametric Curves"
  26.     by Bernard W. Jordan, William J. Lennon and Barry D. Holm, IEEE
  27.     Transaction on Computers Vol C-22, No. 12 December 1973.
  28. */
  29.  
  30. curve(xstart, ystart, xend, yend, direction, a, b, xoff, yoff, val)
  31. int    xstart, ystart, xend, yend, a, b, xoff, yoff;
  32. int    direction, val;
  33. {
  34.     int    dfx, dfy, dfxx, dfyy;
  35.     int    falpha, fx, fy, fxy, absfx, absfy, absfxy;
  36.     int    margin, test_succeed, x, y, deltax, deltay, inc, dec;
  37.     
  38.     if (a == 0 || b == 0) return;
  39.     x    = xstart;
  40.     y    = ystart;
  41.     dfx  = 2 * a * xstart;
  42.     dfy  = 2 * b * ystart;
  43.     dfxx = 2 * a;
  44.     dfyy = 2 * b;
  45.  
  46.     falpha = 0;
  47.     if (direction) {
  48.         inc = 1;  dec = -1;
  49.         }
  50.     else {
  51.         inc = -1;  dec = 1;
  52.         }
  53.     if (xstart == xend && ystart == yend) {
  54.         test_succeed = margin = 1;
  55.         }
  56.     else {
  57.         test_succeed = margin = 3; 
  58.         }
  59.  
  60.     do {
  61.         if (dfy < 0) deltax = inc; else deltax = dec;
  62.         if (dfx < 0) deltay = dec; else deltay = inc;
  63.         fx  = falpha + dfx * deltax + a;
  64.         fy  = falpha + dfy * deltay + b;
  65.         fxy = fx + fy - falpha;
  66.         absfx  = abs(fx); absfy  = abs(fy); absfxy = abs(fxy);
  67.  
  68.         if ((absfxy <= absfx) && (absfxy <= absfy))
  69.         falpha = fxy;
  70.         else if (absfy <= absfx) {
  71.         deltax = 0; falpha = fy;
  72.         }
  73.         else {
  74.         deltay = 0; falpha = fx;
  75.         }
  76.         x = x + deltax;  y = y + deltay;
  77.         pw_put(canvas_pixwin, xoff+x, yoff-y, val);
  78.         dfx = dfx + dfxx * deltax;
  79.         dfy = dfy + dfyy * deltay;
  80.         if (abs(x - xend) < margin && abs(y - yend) < margin) 
  81.         test_succeed--;
  82.         } while (test_succeed);
  83.     }
  84.